let jwt = require('jsonwebtoken');
let userDao = require('../../../dao/userDao');
let code = require('../../../consts/consts');
module.exports = function(app) {
  return new Handler(app);
};

let Handler = function(app) {
  this.app = app;
  this.sessionService = app.get('sessionService');
  this.redis_db = this.app.get('redis_db');
};

Handler.prototype.login = async function(msg, session, next) {
	let token = msg.token;
	if(!token){
		next(null, {
			code: code.FAIL, 
			gameId:code.GAME_ID.HALL,
			msg: code.FAIL_CODE.PARAM});
		return;
	};
	jwt.verify(token,this.app.get('app_sceret').TOKEN_SECRET, async (err,data)=>{
		if(err){
			next(null, {code:  code.FAIL,gameId:code.GAME_ID.HALL, msg: code.FAIL_CODE.TOKEN});
			return;
		};
		if(data){
			console.log('data===>',data);
			let unid = data.unid;
			try {
				let res = await userDao.queryUserByUnid(unid);
				if(res){
					let uid = res.uid;
					console.log('uid=====>',uid);
					this.sessionService.kick(uid,(err,data)=>{
						if(err){ 
							next(null, {code:  code.FAIL, gameId:code.GAME_ID.HALL,msg: code.FAIL_CODE.SESSION_KICK});
							return;
						};
						session.bind(uid, (err,data)=>{
							if(err){
								next(null, {code:  code.FAIL,gameId:code.GAME_ID.HALL, msg: code.FAIL_CODE.SESSION_BIND});
								return;
							}
							session.set('uid',uid);
							session.on('closed', onUserLeave.bind(null, this.app));
							session.pushAll( async ()=>{
								let frontendId = session.frontendId;
								try {
									let user_db = await this.redis_db.hgetallAsync(uid);
									if(!user_db){
										user_db  = {
											serverId:frontendId,
											serverType:'hall',
											tid:0,
											uid:uid
										};
										try {
											await this.redis_db.HMSETAsync(uid,user_db);
											res.serverType = user_db.serverType;
											res.tid = user_db.tid;
											next(null, {code:code.OK_CODE.LOGIN_SUCCESS,gameId:code.GAME_ID.HALL, msg: res});
										} catch (error) {
											next(null, {code: code.FAIL, gameId:code.GAME_ID.HALL,msg: code.FAIL_CODE.USER_NOT_EXIST});
											onUserLeave(this.app,session).bind(this);
										};
									}else{
										res.serverType = user_db.serverType;
										res.tid = user_db.tid;
										next(null, {code:code.OK_CODE.LOGIN_SUCCESS,gameId:code.GAME_ID.HALL, msg: res});
									}
								} catch (error) {
									next(null, {code: code.FAIL, gameId:code.GAME_ID.HALL,msg: code.FAIL_CODE.USER_NOT_EXIST});
									onUserLeave(this.app,session).bind(this);
								}
							});
						});
					});
				}else
				{
					next(null, {code: code.FAIL, gameId:code.GAME_ID.HALL,msg: code.FAIL_CODE.USER_NOT_EXIST});
					return;
				}
			} catch (error) {
				next(null, {code: code.FAIL,gameId:code.GAME_ID.HALL, msg: code.FAIL_CODE.DATA_BASE_ERROR});
				return;
			};
		};
	});
};
let onUserLeave =  async (app, session) =>{
	console.log('session======>leave');
    if (!session || !session.uid) {
        return;
	}
	let uid = session.get('uid');
	let tid = session.get('tid');
	let opts = {
		uid:uid,
		tid:tid,
		reason:code.EXIT_REASON.LEAVE 
	};
	let redis_db = app.get('redis_db');
	let serverId = await redis_db.hgetFieldAsync(uid,'serverId');
	if(serverId == 'ycmj-server-1'){
		if(app.rpc.ycmj && app.rpc.ycmj.ycmjRemote){
			app.rpc.ycmj.ycmjRemote.gameExit(session,opts,(err,data)=>{

			});
		}
	}else
	{
		serverId = 0;
		await redis_db.HMSETFieldAsync(uid,'serverId',serverId);
	}
};

